class Admin::CategoriesController < Admin
  # GET /categories
  # GET /categories.xml
  def index
    @categories = Category.full_list(params[:page])

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @categories }
    end
  end

  # GET /categories/1
  # GET /categories/1.xml
  def show
    @category = Category.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @category }
    end
  end

  # GET /categories/new
  # GET /categories/new.xml
  def new
    @artwork = Artwork.find(params[:artwork_id])
    @category = Category.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @category }
    end
  end

  # GET /categories/1/edit
  def edit
    @category = Category.find(params[:id])
  end

  # POST /categories
  # POST /categories.xml
  def create
    @artwork = Artwork.find(params[:artwork_id])
    english_name = params[:category][:name]
    params[:category].delete(:name)
    @category = Category.new(params[:category])
    @category.parent_id = params[:parent]

    respond_to do |format|
      if @category.save
        CategoryName.create :category_id => @category.id, :name => english_name, :language => 'English'
        
        flash[:notice] = 'Category was successfully created and added to artwork.'
        @artwork.add_category(@category)
        format.html { redirect_to(new_admin_artwork_category_url(@artwork)) }
        format.xml  { render :xml => @category, :status => :created, :location => @category }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @category.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /categories/1
  # PUT /categories/1.xml
  def update
    @category = Category.find(params[:id])

    respond_to do |format|
      if @category.update_attributes(params[:category])
        flash[:notice] = 'Category was successfully updated.'
        format.html { redirect_to(@category) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @category.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /categories/1
  # DELETE /categories/1.xml
  def destroy
    @category = Category.find(params[:id])
    @category.destroy

    respond_to do |format|
      format.html { redirect_to(categories_url) }
      format.xml  { head :ok }
    end
  end  
  
  def add_ajax_name
    @category_name = CategoryName.new(params[:category_name])
    @category_name.category_id = params[:category_id]
    respond_to do |format|
      if @category_name.save
        format.js { render :update do |page|
          page.insert_html :bottom, "category_description_table", {:partial => "admin/categories/edit_name", :object => @category_name}
          page.visual_effect :highlight, "category_description_table", :duration => 2
          page[:category_name_language].value = ''
          page[:category_name_name].value = ''
        end }

      else
        #error
        format.js { render :text => 'error'}
      end
    end
  end
  
  def update_ajax_name
    @category_name = CategoryName.find(params[:id])
    respond_to do |format|
      if @category_name.update_attributes(params[:category_name])
        format.js { render :update do |page|
          page.visual_effect :highlight, 'name_' + @category_name.id.to_s
        end }
      else
        # TODO: error
        format.js { render :update do |page|
          
        end }
      end
    end
  end
  
  def destroy_ajax_name
    @category_name = CategoryName.find(params[:id])
    @category_name.destroy
    
#    category = Category.find(@category_name.category_id)
#    category.last_edited_by = current_user
#    category.save

    respond_to do |format|
      format.html { redirect_to(categories_url) }
      format.xml  { head :ok }
      format.js { render :update do |page|
        page.remove "category_name_#{params[:id]}_row"
        page.visual_effect :highlight, 'category_description_table', :duration => 2
      end }
    end
  end
end
